package com.shijiwei.lifthelper.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.shijiwei.lifthelper.R;
import com.shijiwei.lifthelper.adapter.WeatherAddCityAdapter;

import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by sjw on 2015/12/16.
 */
public class WeatherAddCityActivity extends AppCompatActivity{
    @ViewInject(R.id.gv_addcity_weather)
    private GridView gridView;
    @ViewInject(R.id.iv_cancle)
    private ImageView ivCancle;
    @ViewInject(R.id.et_city)
    private EditText editText;
    @ViewInject(R.id.iv_add)
    private ImageView ivAdd;

    private List<String> citys;
    private WeatherAddCityAdapter adapter;
    private String city;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_weatheraddcity);
        x.view().inject(this);
        initData();
        addListener();
    }

    private void addListener() {
        ivCancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        ivAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                city = editText.getText().toString();
                Intent intent = new Intent();
                intent.putExtra("city",city);
                setResult(RESULT_OK,intent);
                finish();
            }
        });

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TextView textView = (TextView) view.findViewById(R.id.tv_city);
                Intent intent = new Intent();
                intent.putExtra("city",textView.getText().toString());
                setResult(RESULT_OK, intent);
                finish();
            }
        });

    }

    private void initData() {
        citys = new ArrayList<>();
        citys.add("北京");
        citys.add("上海");
        citys.add("广州");
        citys.add("深圳");
        citys.add("天津");
        citys.add("武汉");
        citys.add("沈阳");
        citys.add("重庆");
        citys.add("杭州");
        citys.add("南京");
        citys.add("哈尔滨");
        citys.add("长春");
        citys.add("呼和浩特");
        citys.add("石家庄");
        citys.add("银川");
        citys.add("乌鲁木齐");
        citys.add("拉萨");
        citys.add("西宁");
        citys.add("西安");
        citys.add("兰州");
        citys.add("太原");
        citys.add("昆明");
        citys.add("南宁");
        citys.add("成都");
        citys.add("长沙");
        citys.add("济南");
        citys.add("南昌");
        citys.add("合肥");
        citys.add("郑州");
        citys.add("福州");
        citys.add("贵阳");
        citys.add("海口");
        citys.add("秦皇岛");
        citys.add("桂林");
        citys.add("三亚");
        citys.add("澳门");

        adapter = new WeatherAddCityAdapter(citys,this);
        gridView.setAdapter(adapter);

    }
}
